package com.zfjt.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zfjt.constants.CurrentTimeTools;
import com.zfjt.constants.UUIDGenerator;
import com.zfjt.entity.BasicCheck;
import com.zfjt.packnum.PackNum00009;
import com.zfjt.service.BasicCheckService;

@Controller
public class BasicCheckController
{
	@Autowired
	private BasicCheckService basicCheckService;
	@Autowired
	private FileUploadController fuc;
	
	// 手机端添接口
	@RequestMapping(value = "TelePhoneAdd", method = RequestMethod.POST)
	@ResponseBody
	public BasicCheck addBasicCheckTelephone(HttpSession hseesion,HttpServletRequest request)
	{
		String data =  (String) hseesion.getAttribute("data");
		BasicCheck basicCheck = JSON.parseObject(data,BasicCheck.class); 
		return null;
	}
		
	// 手机端添接口2
	@RequestMapping(value="number1002", method = RequestMethod.POST)
	@ResponseBody
	public BasicCheck addBasicCheckTelephone2( MultipartFile[] res)
	{
		return null;
	}
	
	// 添加一个客户
		@RequestMapping(value="addBasicCheck", method = RequestMethod.POST)
		@ResponseBody
		public Integer addBasicCheck( MultipartHttpServletRequest res)
		{
			BasicCheck basicCheck = new BasicCheck();
			basicCheck.setId(UUIDGenerator.getUUID());
			basicCheck.setAddtime(CurrentTimeTools.getCurrentTime().toString());
			basicCheck.setName(res.getParameter("name"));
			basicCheck.setMobile(res.getParameter("mobile"));
			basicCheck.setPlatenumbers(res.getParameter("platenumbers"));
			basicCheck.setIdentification(res.getParameter("identification"));
			basicCheck.setAccount(res.getParameter("account"));
			basicCheck.setVehicle(res.getParameter("vehicle"));
			basicCheck.setProduct(res.getParameter("product"));
			basicCheck.setGenre(res.getParameter("genre"));
			basicCheck.setCredit(res.getParameter("credit"));
			// 获取前台页面图片名字
			if (res.getFile("ima0201").getSize()!= 0) {
				//存储图片到硬盘,并且返回存储地址
				String saveFilePath = fuc.saveFile(res.getParameter("name"), res, res.getFile("ima0201"));
//				存储图片地址到数据库中
				basicCheck.setIma0201(saveFilePath);
			} 
			if(res.getFile("img0104").getSize()!= 0){
				String saveFilePath = fuc.saveFile(res.getParameter("name"), res, res.getFile("img0104"));
				basicCheck.setImg0104(saveFilePath);
			}
			if(res.getFile("img01P5").getSize()!= 0){
				String saveFilePath = fuc.saveFile(res.getParameter("name"), res, res.getFile("img01P5"));
				basicCheck.setImg01P5(saveFilePath);
			}
			
			Integer insert = basicCheckService.insert(basicCheck);
			return insert;
		}
		
//	跳转到展示页面
	@RequestMapping("gotoBasicCheck")
	public String BasicCheck()
	{
		 return "basicCheck/basicCheckList";
	}
 
//	获得用户数据,并分页显示
	@RequestMapping("listBasicCheckDate")
	@ResponseBody
	public Map<String,Object>  listBasicCheckDate(@RequestParam Integer page, @RequestParam Integer rows)
	{
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		PageHelper.startPage(page, rows);
		List<BasicCheck> listBasicCheck = basicCheckService.listBasicCheck(param);
		PageInfo<BasicCheck> pageInfo = new PageInfo(listBasicCheck);
		jsonMap.put("total", pageInfo.getTotal());
		jsonMap.put("rows", listBasicCheck); 
		return jsonMap;
	}

}
